//
//  PhotoSelectViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "PhotoSelectViewController.h"
#import "PhotoSelectCell.h"

@interface PhotoSelectViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

//@property (strong, nonatomic) NSMutableArray *images;

@end

@implementation PhotoSelectViewController

NSMutableArray *_images;

-(NSMutableArray *)images{
    
    if (!_images) {
        _images = [NSMutableArray array];
    }
    return _images;
    
}

static NSString * const reuseIdentifier = @"PhotoSelectCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //1.监听照片添加和删除的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addImage) name:kAddImg object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteImage:) name:kDeleteImg object:nil];

    
}

#pragma mark -监听照片添加和删除的通知

-(void)addImage{
    
    NSLog(@"addImage");
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//
//    //成为相册的代理,返回他选中的图片
    imagePicker.delegate = self;
//
    [self presentViewController:imagePicker animated:YES completion:nil];
    
    
}

-(void)deleteImage:(NSNotification *)notification{
    
    PhotoSelectCell *cell = notification.object;
    
    //在给cell设置值的时候将indexPath.item设置给他的tag. 然后在发送通知的时候把自己传出去;
    [self.images removeObjectAtIndex:cell.tag];
    
    [self.collectionView reloadData];
    
    
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"didFinishPickingMediaWithInfo%@",info);
//    UIImagePickerControllerMediaType = "public.image";
//    UIImagePickerControllerOriginalImage = "<UIImage: 0x7fb41630c870> size {1668, 2500} orientation 0 scale 1.000000";
//    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=99D53A1F-FEEF-40E1-8BB3-7DD55A43C8B7&ext=JPG";
    
    //1.取出图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
 
    
    //2.储存选中回来的图片
    [self.images addObject:image];
    
    //3.刷新表格
    [self.collectionView reloadData];
    
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    NSLog(@"imagePickerControllerDidCancel");
    [self dismissViewControllerAnimated:YES completion:nil];

}


- (void)viewWillLayoutSubviews{
    
    CGFloat margin = 10 ;
    
    self.flowLayout.minimumLineSpacing = margin;
    
    self.flowLayout.minimumInteritemSpacing = margin;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
    
    //定义列数
    
    CGFloat col = 3;
    
    CGFloat width = (self.view.frame.size.width - (col + 1)*margin)/3.0 ;
    
    CGFloat height = width;
    
    self.flowLayout.itemSize = CGSizeMake(width, height);
    
    
    
    
}

#pragma mark - Navigation
#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.images.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //1.创建cell
    PhotoSelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    //2.设置数据
    if (self.images.count == indexPath.item) {
        cell.cellImage = nil;
    }else{
        cell.cellImage = self.images[indexPath.item];
        cell.tag = indexPath.item;
    }

    
    //3.返回cell
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%tu",indexPath.item);
    
}

@end
